home *** CD-ROM | disk | FTP | other *** search
/ Champak 52 / Volume 52 - JOGO DISK .iso / Games / skipandgouls.swf / scripts / __Packages / sarbakan / utils / PerformanceWatcher.as < prev    next >
Text File  |  2007-10-01  |  6KB  |  133 lines

  1. class sarbakan.utils.PerformanceWatcher
  2. {
  3.    static var DEBUG_DEPTH = 123456;
  4.    static var DEBUG_RIGHT_MARGIN = 0;
  5.    static var DEBUG_TOP_MARGIN = 0;
  6.    static var DEBUG_FONT = "Verdana";
  7.    static var DEBUG_FONT_SIZE = 10;
  8.    static var DEBUG_FONT_COLOR = 16777215;
  9.    static var DEBUG_FONT_SHADOW_COLOR = 0;
  10.    static var aListeners = new Array();
  11.    function PerformanceWatcher()
  12.    {
  13.    }
  14.    static function start(l_nMovieFPS)
  15.    {
  16.       sarbakan.utils.PerformanceWatcher.nMovieFPS = l_nMovieFPS;
  17.       if(!sarbakan.utils.PerformanceWatcher.bActive)
  18.       {
  19.          sarbakan.utils.PerformanceWatcher.bActive = true;
  20.          mx.transitions.OnEnterFrameBeacon.init();
  21.          sarbakan.utils.PerformanceWatcher.oEnterFrameListener = new Object();
  22.          sarbakan.utils.PerformanceWatcher.oEnterFrameListener.onEnterFrame = mx.utils.Delegate.create(sarbakan.utils.PerformanceWatcher,sarbakan.utils.PerformanceWatcher.update);
  23.          MovieClip.addListener(sarbakan.utils.PerformanceWatcher.oEnterFrameListener);
  24.       }
  25.       sarbakan.utils.PerformanceWatcher.nCurrentFPS = sarbakan.utils.PerformanceWatcher.nMovieFPS;
  26.       sarbakan.utils.PerformanceWatcher.reset();
  27.    }
  28.    static function stop()
  29.    {
  30.       if(sarbakan.utils.PerformanceWatcher.bActive)
  31.       {
  32.          sarbakan.utils.PerformanceWatcher.bActive = false;
  33.          MovieClip.removeListener(sarbakan.utils.PerformanceWatcher.oEnterFrameListener);
  34.          sarbakan.utils.PerformanceWatcher.stopDebug();
  35.       }
  36.    }
  37.    static function addListener(l_listener, l_nMinFPS, l_bDelete)
  38.    {
  39.       sarbakan.utils.PerformanceWatcher.aListeners.push({listener:l_listener,nMinFPS:l_nMinFPS,bDelete:l_bDelete});
  40.    }
  41.    static function removeListener(l_listener)
  42.    {
  43.       for(var _loc2_ in sarbakan.utils.PerformanceWatcher.aListeners)
  44.       {
  45.          if(sarbakan.utils.PerformanceWatcher.aListeners[_loc2_].listener == l_listener)
  46.          {
  47.             sarbakan.utils.PerformanceWatcher.aListeners.splice(_loc2_,1);
  48.          }
  49.       }
  50.    }
  51.    static function get fps()
  52.    {
  53.       return sarbakan.utils.PerformanceWatcher.nCurrentFPS;
  54.    }
  55.    static function startDebug(l_mcTimeline)
  56.    {
  57.       sarbakan.utils.PerformanceWatcher.mcDebug = l_mcTimeline.createEmptyMovieClip("PerformanceWatcherDebug",sarbakan.utils.PerformanceWatcher.DEBUG_DEPTH);
  58.       sarbakan.utils.PerformanceWatcher.mcDebug._x = Stage.width - sarbakan.utils.PerformanceWatcher.DEBUG_RIGHT_MARGIN - 50;
  59.       sarbakan.utils.PerformanceWatcher.mcDebug._y = sarbakan.utils.PerformanceWatcher.DEBUG_TOP_MARGIN;
  60.       sarbakan.utils.PerformanceWatcher.mcDebug.createTextField("field",2,0,0,50,50);
  61.       var _loc1_ = new TextFormat();
  62.       _loc1_.font = sarbakan.utils.PerformanceWatcher.DEBUG_FONT;
  63.       _loc1_.size = sarbakan.utils.PerformanceWatcher.DEBUG_FONT_SIZE;
  64.       _loc1_.color = sarbakan.utils.PerformanceWatcher.DEBUG_FONT_COLOR;
  65.       _loc1_.align = "right";
  66.       sarbakan.utils.PerformanceWatcher.mcDebug.field.setNewTextFormat(_loc1_);
  67.       sarbakan.utils.PerformanceWatcher.mcDebug.field.selectable = false;
  68.       sarbakan.utils.PerformanceWatcher.mcDebug.createTextField("fieldShadow",1,1,1,50,50);
  69.       var _loc2_ = new TextFormat();
  70.       _loc2_.font = sarbakan.utils.PerformanceWatcher.DEBUG_FONT;
  71.       _loc2_.size = sarbakan.utils.PerformanceWatcher.DEBUG_FONT_SIZE;
  72.       _loc2_.color = sarbakan.utils.PerformanceWatcher.DEBUG_FONT_SHADOW_COLOR;
  73.       _loc2_.align = "right";
  74.       sarbakan.utils.PerformanceWatcher.mcDebug.fieldShadow.setNewTextFormat(_loc2_);
  75.       sarbakan.utils.PerformanceWatcher.mcDebug.fieldShadow.selectable = false;
  76.    }
  77.    static function stopDebug()
  78.    {
  79.       if(sarbakan.utils.PerformanceWatcher.mcDebug != undefined)
  80.       {
  81.          sarbakan.utils.PerformanceWatcher.mcDebug.removeMovieClip();
  82.       }
  83.    }
  84.    static function update()
  85.    {
  86.       if(++sarbakan.utils.PerformanceWatcher.nUpdateCount == sarbakan.utils.PerformanceWatcher.nMovieFPS)
  87.       {
  88.          sarbakan.utils.PerformanceWatcher.computeFrameRate();
  89.          sarbakan.utils.PerformanceWatcher.updateDebug();
  90.          sarbakan.utils.PerformanceWatcher.notifyListeners();
  91.       }
  92.    }
  93.    static function reset()
  94.    {
  95.       sarbakan.utils.PerformanceWatcher.nUpdateCount = 0;
  96.       sarbakan.utils.PerformanceWatcher.nLastGetTimer = getTimer();
  97.    }
  98.    static function computeFrameRate()
  99.    {
  100.       var _loc1_ = getTimer() - sarbakan.utils.PerformanceWatcher.nLastGetTimer;
  101.       var _loc2_ = _loc1_ / 1000;
  102.       sarbakan.utils.PerformanceWatcher.nCurrentFPS = Math.round(sarbakan.utils.PerformanceWatcher.nMovieFPS / _loc2_);
  103.       sarbakan.utils.PerformanceWatcher.reset();
  104.    }
  105.    static function notifyListeners()
  106.    {
  107.       var _loc1_ = new Array();
  108.       for(var _loc2_ in sarbakan.utils.PerformanceWatcher.aListeners)
  109.       {
  110.          if(sarbakan.utils.PerformanceWatcher.aListeners[_loc2_].nMinFPS >= sarbakan.utils.PerformanceWatcher.nCurrentFPS)
  111.          {
  112.             sarbakan.utils.PerformanceWatcher.aListeners[_loc2_].listener(sarbakan.utils.PerformanceWatcher.nCurrentFPS);
  113.             if(sarbakan.utils.PerformanceWatcher.aListeners[_loc2_].bDelete)
  114.             {
  115.                _loc1_.push(sarbakan.utils.PerformanceWatcher.aListeners[_loc2_].listener);
  116.             }
  117.          }
  118.       }
  119.       for(_loc2_ in _loc1_)
  120.       {
  121.          sarbakan.utils.PerformanceWatcher.removeListener(_loc1_[_loc2_]);
  122.       }
  123.    }
  124.    static function updateDebug()
  125.    {
  126.       if(sarbakan.utils.PerformanceWatcher.mcDebug != undefined)
  127.       {
  128.          sarbakan.utils.PerformanceWatcher.mcDebug.field.text = sarbakan.utils.PerformanceWatcher.nCurrentFPS + " fps";
  129.          sarbakan.utils.PerformanceWatcher.mcDebug.fieldShadow.text = sarbakan.utils.PerformanceWatcher.nCurrentFPS + " fps";
  130.       }
  131.    }
  132. }
  133.